#!/bin/bash

export HIVE_HOME=/usr/bin/hive

if [ $# == 1 ]

then

   dateStr=$1

else
   dateStr=`date -d'-1 day' +'%Y-%m-%d'`
fi


echo ${dateStr}

hiveSql="
-- web_chat_ems
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

set hive.exec.compress.intermediate=true;
set hive.exec.compress.output=true;

set hive.exec.orc.compression.strategy=COMPRESSION;

set hive.enforce.bucketing=true;

set hive.enforce.sorting=true;

insert overwrite table zx_dwd.dwd_web_chat_ems partition(yearinfo,quarterinfo,monthinfo,dayinfo)
select
    id, 
    create_date_time, 
    session_id, 
    sid, 
    create_time, 
    seo_source, 
    seo_keywords, 
    ip, 
    area, 
    country, 
    province, 
    city, 
    origin_channel, 
    user1, 
    manual_time, 
    begin_time, 
    end_time, 
    last_customer_msg_time_stamp, 
    last_agent_msg_time_stamp, 
    reply_msg_count, 
    msg_count, 
    browser_name, 
    os_info, 
    substr(create_time,12,2) as hourinfo,
    substr(create_time,1,4) as yearinfo,
    quarter(create_time) as quarterinfo,
    substr(create_time,6,2) as monthinfo,
    substr(create_time,9,2) as dayinfo
from  zx_ods.web_chat_ems
where dt = '${dateStr}'
	and create_time is not null and (session_id is not null or sid is not null or ip is not null);


-- web_chat_text_ems
insert overwrite table zx_dwd.dwd_web_chat_text_ems partition(dt)
select
   *
from  zx_ods.web_chat_text_ems where dt = '${dateStr}';"


${HIVE_HOME} -S -e "${hiveSql}"